<?php 
class Router
{
	private static $pi;
	CONST MAX_DEEP = 10;
    /**
     * http://localhost/member2.xx/fwe/.dhfl?lsj=2
     * @return 
object(stdClass)#1 (10) {
  ["dirname"]=>
  string(15) "/member2.xx/fwe"
  ["basename"]=>
  string(11) ".dhfl?lsj=2"
  ["extension"]=>
  string(10) "dhfl?lsj=2"
  ["filename"]=>
  string(0) ""
  ["ext"]=>
  string(4) "dhfl"
  ["query"]=>
  string(5) "lsj=2"
  ["fn"]=>
  string(0) ""
  ["path"]=>
  string(15) "/member2.xx/fwe"
  ["uri"]=>
  string(27) "/member2.xx/fwe/.dhfl?lsj=2"
}
     */
    public static function request()
    {

        if(self::$pi) return self::$pi;

		$uri = $_SERVER['REQUEST_URI'];
        PRE_REQUEST_URI && 0===strpos($uri,PRE_REQUEST_URI) && $uri =substr($uri,strlen(PRE_REQUEST_URI));

		$pi = pathinfo($uri);
		$pi['dirname'] = preg_replace('/\/+/','/',$pi['dirname']);
		isset($pi['extension']) && $extArr=parse_url($pi['extension']);
		isset($extArr['path']) && $pi['ext'] = '.'.$extArr['path'];
		isset($extArr['query']) && $pi['query'] = @$extArr['query'];
		isset($pi['filename']) && $fnArr=parse_url($pi['filename']);
		isset($fnArr['path']) && $pi['fn'] = @$fnArr['path'];
		$pi['path'] = $pi['dirname'].(@$pi['fn']?'/'.$pi['fn']:'');
		$pi['path'] = str_replace(array('\\','//'),array('','/'),$pi['path']);
		$pi['uri'] = $uri;
		$pi['lang'] = '';
		$pi['curr'] = '';
		
        if(substr_count($pi['path'],'/') > self::MAX_DEEP) throw new Exception('Request Out of Arrange !');

		self::$pi = (object)$pi;
		if(substr(self::$pi->path,0,6)=='/lang-') self::parseLang();
		
		define('IS_POST',strtoupper($_SERVER['REQUEST_METHOD']) == 'POST');
		define('IS_AJAX',!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

		return self::$pi;
	
    }
    
    private static function parseLang()
    {
        $a = explode('/',self::$pi->path);
        array_shift($a);
        $l = array_shift($a);
        $la = explode('-',$l);
        if(count($la)>3) return ;
        isset($la[1]) && System::$langAllows && in_array($la[1],System::$langAllows) && self::$pi->lang = $la[1];
        isset($la[2]) && System::$currAllows && in_array($la[2],System::$currAllows) && self::$pi->curr = $la[2];
        self::$pi->path = '/'.implode('/',$a);
    }
}
